import 'package:flutter/material.dart';
import 'package:tiktokadult/assets_tool/app_colors.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';

class AICommonTabbar extends StatefulWidget {
  final TabController? tabController;
  const AICommonTabbar({super.key, this.tabController});

  @override
  State<AICommonTabbar> createState() => _AICommonTabbarState();
}

class _AICommonTabbarState extends State<AICommonTabbar> {
  late final tabs = ['视频换脸', '图片换脸', '科技脱衣'];
  late final tabIcons = [
    'ai_video_face.png'.aiPath,
    'ai_pic_face.png'.aiPath,
    'ai_address.png'.aiPath,
  ];
  TabController? get tabController => widget.tabController;

  @override
  void initState() {
    super.initState();
    widget.tabController?.addListener(() {
      if (!(widget.tabController?.indexIsChanging ?? false)) {
        setState(() {});
      }
    });
  }

  @override
  void dispose() {
    widget.tabController?.removeListener(() {});
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: List.generate(tabs.length, (index) {
        final isSelect = tabController?.index == index;
        return GestureDetector(
          onTap: () {
            tabController?.index = index;
          },
          child: Container(
            decoration: BoxDecoration(
              color: isSelect ? AppColors.actionRed : Colors.white.withOpacity(.1),
              borderRadius: BorderRadius.circular(3),
            ),
            padding: EdgeInsets.symmetric(horizontal: 6, vertical: 5),
            margin: EdgeInsets.only(right: index == tabs.length - 1 ? 0 : 10),
            child: Row(
              children: [
                Image.asset(
                  tabIcons[index],
                  width: 20,
                  height: 20,
                ),
                6.sizeBoxW,
                Text(
                  tabs[index],
                  style: TextStyle(color: Colors.white, fontSize: 12),
                )
              ],
            ),
          ),
        );
      }),
    );
  }
}
